﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class web_pages_nhatuyendung_edit_vieclamtrong : System.Web.UI.Page
{
    public string ma_dn;
    public DataTable dtt;
    Common comm = new Common();
    
    public ViecLamTrong vltComm = new ViecLamTrong();
    public objViecLamTrong objVLT;

    public string[] noilamviec;
    public string[] yeucaunguoi = null;
    public string[] yeucaunguoiText;
    public string[] yeucauhokhau;
    public string[] yeucauchuyenmon;
    public string[] yeucauchuyenmonText;
    public string[] yeucaunghiepvu;
    public string[] yeucaunghiepvuText;
    public string[] yeucaukynangmem;
    public string[] yeucaukynangmemText;
    DateTime dat = DateTime.Now;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "Nhà tuyển dụng " + ConfigurationManager.AppSettings["centername"];
        if (Session["NTDuser"] == null)
        {
            Response.Redirect("nhatuyendung.aspx");
        }
        ma_dn = Session["NTDid"].ToString();

        string ma_ho_so = Request.QueryString["vltid"];

        chk_luong_thoa_thuan.Attributes.Add("onClick", "$('#muc_luong_div').toggle();");
        if (!IsPostBack)
        {
            LoadThoiGianLamViec();
            LoadTP();
            LoadKNMem();
            LoadLoaiVLT();
            LoadNguonVLT();
            initForm(ma_ho_so);
           
        }

    }
    protected void initForm(String ma_ho_so) {
        objVLT = ViecLamTrong.getViecLamTrong(ma_ho_so);

        txt_chuc_danh_hidden.Value = objVLT.ma_chuc_danh;
        txt_giay_to.Text = objVLT.yeu_cau_giay_to;
        txt_luong_den.Value = objVLT.luong_den;
        txt_luong_tu.Value = objVLT.luong_tu;
        txt_mo_ta_cong_viec.Text = objVLT.mo_ta;
        txt_ngay_cham_duc.SelectedDate = objVLT.ngay_het_han;
        txt_ngay_du_tuyen.SelectedDate = objVLT.ngay_du_tuyen;
        txt_ngay_nhan_ho_so.SelectedDate = objVLT.ngay_nhan;
        txt_noi_du_tuyen.Text = objVLT.noi_du_tuyen;
        txt_noi_nhan_ho_so.Text = objVLT.noi_nop_ho_so;
        txt_quyen_loi.Text = objVLT.quyen_loi;
        txt_so_luong_tuyen.Value = objVLT.so_luong_tuyen;
        txt_tieu_de.Text = objVLT.tieu_de_ho_so;
        txt_vi_tri_tuyen_dung.Text = objVLT.vi_tri_tuyen_dung;
        txt_yeu_cau_khac.Text = objVLT.yeu_cau_khac;

        if (objVLT.luong_tu == objVLT.luong_den && objVLT.luong_den == 0)
            chk_luong_thoa_thuan.Checked = true;
     
        chk_kich_hoat.Checked = objVLT.kich_hoat.Value;

        cbb_loai_vlt.Items.FindByValue(objVLT.ma_loai_vlt).Selected = true; 
        cbb_nguon_vlt.Items.FindByValue(objVLT.ma_nguon_vlt).Selected = true;

        cbb_thoi_gian_lam_viec.Items.FindByValue(objVLT.ma_thoi_gian).Selected = true;
        
        noilamviec = objVLT.noi_lam_viec.Split(new char[] { '#' });
        yeucauhokhau = objVLT.ho_khau.Split(new char[] { '@' });

        if (objVLT.listYeuCauVeNguoi.Count > 0)
        {
            yeucaunguoi = new string[objVLT.listYeuCauVeNguoi.Count];
            yeucaunguoiText = new string[objVLT.listYeuCauVeNguoi.Count];
            int i = -1;
            string sGioiTinh = "";
            foreach (objYeuCauVeNguoi _objNguoi in objVLT.listYeuCauVeNguoi)
            {
                i++;

                if (_objNguoi.gioi_tinh == "1") sGioiTinh = "Nam";
                else if (_objNguoi.gioi_tinh == "0") sGioiTinh = "Nữ";
                else sGioiTinh = "Nam/Nữ";
                yeucaunguoi[i] = _objNguoi.gioi_tinh + "#" + _objNguoi.so_luong + "#" + _objNguoi.nang_tu + "#" + _objNguoi.nang_den + "#" + _objNguoi.cao_tu + "#" + _objNguoi.cao_den + "#" + _objNguoi.tuoi_tu + "#" + _objNguoi.tuoi_den + "#" + _objNguoi.mo_ta;
                yeucaunguoiText[i] = sGioiTinh + ", Số lượng :" + _objNguoi.so_luong + ", Cân nặng :" + _objNguoi.nang_tu + " - " + _objNguoi.nang_den + ", Chiều cao : " + _objNguoi.cao_tu + " - " + _objNguoi.cao_den + ", Độ tuổi : " + _objNguoi.tuoi_tu + " - " + _objNguoi.tuoi_den + ". " + _objNguoi.mo_ta;
            }
        }
        if (objVLT.listYeuCauChuyenMon.Count > 0)
        {
            yeucauchuyenmon = new string[objVLT.listYeuCauChuyenMon.Count];
            yeucauchuyenmonText = new string[objVLT.listYeuCauChuyenMon.Count];
            int i = -1;
            foreach (objYeuCauChuyenMon _obj in objVLT.listYeuCauChuyenMon)
            {
                i++;
                yeucauchuyenmon[i] = _obj.ma_chuyen_mon +"#"+_obj.kinh_nghiem+"#"+_obj.ma_hoc_van+"#"+_obj.ma_hang_tn.Trim();
                yeucauchuyenmonText[i] = vltComm.getTenChuyenMon(_obj.ma_chuyen_mon)+", kinh nghiệm  "+_obj.kinh_nghiem+" tháng, Học Vấn "+ vltComm.getTenHocVan(_obj.ma_hoc_van)+" hạng "+  vltComm.getTenHangTN(_obj.ma_hang_tn.Trim())+".";
            }
        }
        if (objVLT.listYeuCauNghiepVu.Count > 0)
        {
            yeucaunghiepvu = new string[objVLT.listYeuCauNghiepVu.Count];
            yeucaunghiepvuText = new string[objVLT.listYeuCauNghiepVu.Count];
            int i = -1;
            foreach (objYeuCauNghiepVu _obj in objVLT.listYeuCauNghiepVu)
            {
                i++;
                yeucaunghiepvu[i] = _obj.ma_nghiep_vu +"#"+_obj.ma_hang;
                yeucaunghiepvuText[i] = vltComm.getTenNghiepVu(_obj.ma_nghiep_vu)+" hạng "+vltComm.getTenHangTN(_obj.ma_hang.Trim());
            }
        }
        if (objVLT.listYeuCauKhaNangMem.Count > 0)
        {
            yeucaukynangmem = new string[objVLT.listYeuCauKhaNangMem.Count];
            yeucaukynangmemText = new string[objVLT.listYeuCauKhaNangMem.Count];
            int i = -1;
            foreach (objYeuCauKhaNangMem _obj in objVLT.listYeuCauKhaNangMem)
            {
                i++;
                yeucaukynangmem[i] = _obj.ma_kn_mem;
                yeucaukynangmemText[i] = vltComm.getTenKNM(_obj.ma_kn_mem.Trim());
            }
        }

    
    }

    protected void LoadThoiGianLamViec()
    {
        Common comm = new Common();

        string sqlComm = "Select ma_thoi_gian_lv, ten_thoi_gian_lv from DanhMuc_thoi_gian_lam_viec   ";
        DataTable dtt = comm.getDataTable(sqlComm);
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            cbb_thoi_gian_lam_viec.Items.Add(new ListItem(dtt.Rows[i]["ten_thoi_gian_lv"].ToString(), dtt.Rows[i]["ma_thoi_gian_lv"].ToString()));
        }
    }
    protected void LoadTP()
    {
        Common comm = new Common();

        string sqlComm = "Select ma_dia_chi, ten_dia_chi from DanhMuc_dia_chi where muc=0 order by ten_dia_chi ";
        DataTable dtt = comm.getDataTable(sqlComm);
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            cbb_list_ho_khau.Items.Add(new ListItem(dtt.Rows[i]["ten_dia_chi"].ToString(), dtt.Rows[i]["ma_dia_chi"].ToString()));
            cbb_list_noilamviec.Items.Add(new ListItem(dtt.Rows[i]["ten_dia_chi"].ToString(), dtt.Rows[i]["ma_dia_chi"].ToString()));
            cbb_tinhthanh.Items.Add(new ListItem(dtt.Rows[i]["ten_dia_chi"].ToString(), dtt.Rows[i]["ma_dia_chi"].ToString()));
        }
    }
    protected void LoadKNMem()
    {
        Common comm = new Common();

        string sqlComm = "Select ma_kn_mem, ten_kn_mem from DanhMuc_kha_nang_mem where kich_hoat = 1  ";
        DataTable dtt = comm.getDataTable(sqlComm);
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            cbb_list_ky_nang_mem.Items.Add(new ListItem(dtt.Rows[i]["ten_kn_mem"].ToString(), dtt.Rows[i]["ma_kn_mem"].ToString()));
        }
    }


    protected void LoadLoaiVLT()
    {
        Common comm = new Common();

        string sqlComm = "Select ma_loai_vlt, ten_loai_vlt from DanhMuc_loai_viec_lam_trong  ";
        DataTable dtt = comm.getDataTable(sqlComm);
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            cbb_loai_vlt.Items.Add(new ListItem(dtt.Rows[i]["ten_loai_vlt"].ToString(), dtt.Rows[i]["ma_loai_vlt"].ToString()));
        }
    }
    protected void LoadNguonVLT()
    {
        Common comm = new Common();

        string sqlComm = "Select ma_nguon_vlt, ten_nguon_vlt from DanhMuc_nguon_viec_lam_trong  ";
        DataTable dtt = comm.getDataTable(sqlComm);
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            cbb_nguon_vlt.Items.Add(new ListItem(dtt.Rows[i]["ten_nguon_vlt"].ToString(), dtt.Rows[i]["ma_nguon_vlt"].ToString()));
        }
    }

    protected void btn_ok_Click(object sender, EventArgs e)
    {
        Common comm = new Common();
        objViecLamTrong objVLT = new objViecLamTrong();
        string ma_ho_so = Request.QueryString["vltid"];
        objVLT = ViecLamTrong.getViecLamTrong(ma_ho_so);
        objVLT.ho_khau = txt_list_hokhau_hidden.Value;
        objVLT.kich_hoat = chk_kich_hoat.Checked;
       // objVLT.kieu_tien = cbb_kieu_tien.SelectedValue;
        objVLT.luong_den = (chk_luong_thoa_thuan.Checked) ? 0 : (float)txt_luong_den.Value;
        objVLT.luong_tu = (chk_luong_thoa_thuan.Checked) ? 0 : (float)txt_luong_tu.Value;
        objVLT.ma_chuc_danh = txt_chuc_danh_hidden.Value;
        objVLT.ma_doanh_nghiep = Session["NTDid"].ToString();
        objVLT.ma_loai_vlt = cbb_loai_vlt.SelectedValue;
        objVLT.ma_nguon_vlt = cbb_nguon_vlt.SelectedValue;

        objVLT.ma_thoi_gian = cbb_thoi_gian_lam_viec.SelectedValue;
        objVLT.mo_ta = comm.TextFormat(txt_mo_ta_cong_viec.Text);
        objVLT.ngay_dang = DateTime.Now;
        objVLT.ngay_du_tuyen = txt_ngay_du_tuyen.SelectedDate.Value;
        objVLT.ngay_het_han = txt_ngay_cham_duc.SelectedDate.Value;
        objVLT.ngay_nhan = txt_ngay_nhan_ho_so.SelectedDate.Value;
        objVLT.noi_du_tuyen = comm.TextFormat(txt_noi_du_tuyen.Text);
        objVLT.noi_lam_viec = txt_list_noilamviec_hidden.Value;
        objVLT.noi_nop_ho_so = comm.TextFormat(txt_noi_nhan_ho_so.Text);
        objVLT.quyen_loi = comm.TextFormat(txt_quyen_loi.Text);
        objVLT.so_luong_tuyen = Convert.ToInt32(txt_so_luong_tuyen.Value);
        objVLT.yeu_cau_giay_to = comm.TextFormat(txt_giay_to.Text);
        objVLT.tieu_de_ho_so = comm.TextFormat(txt_tieu_de.Text);
        objVLT.vi_tri_tuyen_dung = comm.TextFormat(txt_vi_tri_tuyen_dung.Text);
        objVLT.yeu_cau_khac = comm.TextFormat(txt_yeu_cau_khac.Text);
        

        char[] sl1 = new char[] { '@' };
        char[] sl2 = new char[] { '#' };
        string tmp = "";
        string[] tmps1;
        string[] tmps2;
        #region MyRegion
        // Yeu cau chuyen mon
        objYeuCauChuyenMon objYCM;
        objVLT.listYeuCauChuyenMon.Clear();
        if (txt_cbb_list_chuyen_mon_hidden.Value.Length > 3)
        {
            tmp = txt_cbb_list_chuyen_mon_hidden.Value;
            tmps1 = tmp.Split(sl1);
            for (int i = 0; i < tmps1.Length - 1; i++)
            {
                objYCM = new objYeuCauChuyenMon();
                tmps2 = tmps1[i].Split(sl2);
                objYCM.ma_chuyen_mon = tmps2[0];
                objYCM.kinh_nghiem = Convert.ToInt32(tmps2[1]);
                objYCM.ma_hoc_van = tmps2[2];
                objYCM.ma_hang_tn = tmps2[3];
                objVLT.listYeuCauChuyenMon.Add(objYCM);
            }
        }
        #endregion
        #region MyRegion
        // Yeu cau người
        // @gioitinh#so luong#nang_tu#nang den# cao tu# cao_den#tuoi tu# tuoiden#mota

        objYeuCauVeNguoi objYCN;
        objVLT.listYeuCauVeNguoi.Clear();
        if (txt_list_con_nguoi_hidden.Value.Length > 3)
        {
            tmp = txt_list_con_nguoi_hidden.Value;
            tmps1 = tmp.Split(sl1);
            for (int i = 0; i < tmps1.Length - 1; i++)
            {
                objYCN = new objYeuCauVeNguoi();
                tmps2 = tmps1[i].Split(sl2);
                objYCN.gioi_tinh = tmps2[0].ToString();
                objYCN.so_luong = tmps2[1];
                objYCN.nang_tu = Convert.ToInt32(tmps2[2]);
                objYCN.nang_den = Convert.ToInt32(tmps2[3]);
                objYCN.cao_tu = Convert.ToInt32(tmps2[4]);
                objYCN.cao_den = Convert.ToInt32(tmps2[5]);
                objYCN.tuoi_tu = Convert.ToInt32(tmps2[6]);
                objYCN.tuoi_den = Convert.ToInt32(tmps2[7]);
                try{
                objYCN.mo_ta = tmps2[8];
                }catch{}
                objVLT.listYeuCauVeNguoi.Add(objYCN);
            }
        }
        #endregion
        #region MyRegion
        // Yeu kha nang mem
        // 

        objYeuCauKhaNangMem objYCKNM;
        objVLT.listYeuCauKhaNangMem.Clear();
        if (cbb_list_ky_nang_mem_hidden.Value.Length > 3)
        {
            tmp = cbb_list_ky_nang_mem_hidden.Value;
            tmps1 = tmp.Split(sl1);
            for (int i = 0; i < tmps1.Length - 1; i++)
            {
                objYCKNM = new objYeuCauKhaNangMem();

                objYCKNM.ma_kn_mem = tmps1[i];

                objVLT.listYeuCauKhaNangMem.Add(objYCKNM);
            }
        }
        #endregion
        #region MyRegion
        // Yeu nghiep vu

        objYeuCauNghiepVu objYCNV;
        objVLT.listYeuCauNghiepVu.Clear();
        if (txt_list_nghiepvu_hidden.Value.Length > 3)
        {
            tmp = txt_list_nghiepvu_hidden.Value;
            tmps1 = tmp.Split(sl1);
            for (int i = 0; i < tmps1.Length - 1; i++)
            {
                objYCNV = new objYeuCauNghiepVu();
                tmps2 = tmps1[i].Split(sl2);
                objYCNV.ma_nghiep_vu = tmps2[0];
                objYCNV.ma_hang = tmps2[1];
                objVLT.listYeuCauNghiepVu.Add(objYCNV);
            }
        }
        #endregion
        #region MyRegion
        // Yeu giay_to

        //objYeuCauGiayTo objYCGT;
        //if (cbb_list_giay_to_hidden.Value.Length > 3)
        //{
        //    tmp = cbb_list_giay_to_hidden.Value;
        //    tmps1 = tmp.Split(sl1);
        //    for (int i = 0; i < tmps1.Length-1; i++)
        //    {
        //        objYCGT = new objYeuCauGiayTo();
        //        objYCGT.ma_giay_to = tmps1[i];
        //        objVLT.listYeuCauGiayTo.Add(objYCGT);
        //    }
        //}
        #endregion

        ViecLamTrong vltCommon = new ViecLamTrong();
        vltCommon.updateVLT(objVLT);
		//Request.UserHostAddress+DateTime.Now.Date.ToShortDateString()
        //Common commn = new Common();
        //String ip=Request.ServerVariables["REMOTE_ADDR"].ToString();
        //String strr="update Vieclamtrong_ho_so_tuyen_dung set nguon_cap_nhat='"+ip+"', cap_nhat='"+DateTime.Now.Date.ToShortDateString()+"' where ma_ho_so='"+Request.QueryString["vltid"]+"'; select ma_ho_so from Vieclamtrong_ho_so_tuyen_dung where ma_ho_so='12'";
        //commn.getDataTable(strr);
        Response.Redirect("notice.aspx?backurl=NTV_Home");

    }
}
